Skip to content

Conversation

@RiverDave
Copy link
Contributor

This aims to fix a portion of #122480. Simply added a check to look for either compile time initialized variables or static stored variables.

@llvmbot
Copy link
Member

llvmbot commented Mar 2, 2025

@llvm/pr-subscribers-clang-tools-extra

@llvm/pr-subscribers-clang-tidy

Author: David Rivera (RiverDave)

Changes

This aims to fix a portion of #122480. Simply added a check to look for either compile time initialized variables or static stored variables.


Full diff: https://github.com/llvm/llvm-project/pull/129425.diff

3 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp (+3-1)
  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp (+19)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
index 6c06b0af342f6..5b0b9b59d4e3b 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
@@ -194,6 +194,8 @@ void UseDefaultMemberInitCheck::storeOptions(
 }
 
 void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) {
+  auto ConstExpRef = varDecl(anyOf(isConstexpr(), isStaticStorageClass()));
+
   auto InitBase =
       anyOf(stringLiteral(), characterLiteral(), integerLiteral(),
             unaryOperator(hasAnyOperatorName("+", "-"),
@@ -202,7 +204,7 @@ void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) {
             unaryOperator(hasAnyOperatorName("+", "-"),
                           hasUnaryOperand(floatLiteral())),
             cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(),
-            declRefExpr(to(enumConstantDecl())));
+            declRefExpr(to(anyOf(enumConstantDecl(), ConstExpRef))));
 
   auto Init =
       anyOf(initListExpr(anyOf(allOf(initCountIs(1), hasInit(0, InitBase)),
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 07a79d6bbe807..3a9da3fda86a5 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -128,6 +128,10 @@ Changes in existing checks
   <clang-tidy/checks/misc/redundant-expression>` check by providing additional
   examples and fixing some macro related false positives.
 
+- Improved :doc:`modernize-use-default-member-init
+  <clang-tidy/checks/modernize/use-default-member-init>` check by Adding check on
+  constexpr & static values on member initialization.
+
 - Improved :doc:`performance/unnecessary-value-param
   <clang-tidy/checks/performance/unnecessary-value-param>` check performance by
   tolerating fix-it breaking compilation when functions is used as pointers 
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp
index 81c980e0217e6..c1a35516e001e 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp
@@ -518,3 +518,22 @@ class ArrayBraceInitMultipleValues {
 };
 
 } // namespace PR63285
+
+namespace PR122480 {
+
+  static int STATIC_VAL = 23;
+  static constexpr const char* CONSTEXPR_REF = "Static";
+
+  class StaticConstExprInit {
+
+    StaticConstExprInit() : a{CONSTEXPR_REF}, b{STATIC_VAL}{}
+    // CHECK-FIXES: StaticConstExprInit()  {}
+    const char* a;
+    // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use default member initializer for 'a' [modernize-use-default-member-init]
+    // CHECK-FIXES: const char* a{CONSTEXPR_REF};
+    int b;
+    // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use default member initializer for 'b' [modernize-use-default-member-init]
+    // CHECK-FIXES: int b{STATIC_VAL};
+  };
+
+} //namespace PR122480
\ No newline at end of file

@RiverDave RiverDave force-pushed the modernize/modernize-use-default-member-init_122480_3 branch from f3e63b3 to ab06aa4 Compare March 2, 2025 17:08
@RiverDave RiverDave force-pushed the modernize/modernize-use-default-member-init_122480_3 branch from ab06aa4 to 2b4d70b Compare March 2, 2025 17:26
@RiverDave
Copy link
Contributor Author

Ping

@RiverDave RiverDave force-pushed the modernize/modernize-use-default-member-init_122480_3 branch from 2b4d70b to 4f25319 Compare March 9, 2025 07:32
Copy link
Member

@PiotrZSL PiotrZSL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except small nit, in tests, looks fine.

@RiverDave RiverDave force-pushed the modernize/modernize-use-default-member-init_122480_3 branch from 4f25319 to d0f3af2 Compare March 10, 2025 21:22
…ization in modernize-use-default-member-init
@RiverDave RiverDave force-pushed the modernize/modernize-use-default-member-init_122480_3 branch from d0f3af2 to ec6b41f Compare March 10, 2025 21:25
@RiverDave
Copy link
Contributor Author

Except small nit, in tests, looks fine.

Amazing, your comments have been addressed

@PiotrZSL
Copy link
Member

@RiverDave Unless you want this to be merged using github generated private email, change your email privacy settings.

@RiverDave
Copy link
Contributor Author

RiverDave commented Mar 11, 2025

@RiverDave Unless you want this to be merged using github generated private email, change your email privacy settings.

My email should be public now. thx for pointing out

@PiotrZSL PiotrZSL merged commit 517c677 into llvm:main Mar 11, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants